package com.rover.system.controller;

import com.common.UploadFile;
import com.grid.DataGrid;
import com.grid.Page;
import com.rover.system.entity.Attachment;
import com.rover.system.service.AttachmentService;
import com.utils.HqlGenerateUtil;
import com.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-9-21
 * Time: 下午2:22
 * 附件
 */

@Controller
@RequestMapping("/attachmentController")
public class AttachmentController {
    @Autowired
    private AttachmentService service;

    // 用户管理
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page,
                                     HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            HqlGenerateUtil.installHql(request,  Attachment.class, page);
            DataGrid dataGrid = service.query(page);
            map = JsonUtil.JSONString(dataGrid.getResult(),
                    dataGrid.getTotalCount());
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    // 用户管理
    @RequestMapping("/attachment")
    @ResponseBody
    public Map<String, Object> attachment(HttpServletRequest request, Page page,
                                     HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Attachment attachment =new Attachment();
            attachment.setBusinessKey(request.getParameter("businessKey"));
            UploadFile uf = new UploadFile(request, attachment);
            uf.setCusPath("files");
            uf.setSwfpath("swfpath");
            service.uploadFile(uf);
            //service.save(attachment);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

}
